﻿using SDF.Core.Infrastructure.DependencyManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using SDF.Core.Configuration;
using SDF.Core.Infrastructure;
using SDF.Web.Framework.Mvc;
using SDF.Plugin.Misc.SAPAdapter.Data;
using SDF.Data;
using SDF.Plugin.Misc.SAPAdapter.Domain;
using SDF.Core.Data;
using Autofac.Core;
using SDF.Plugin.Misc.SAPAdapter.Services;

namespace SDF.Plugin.Misc.SAPAdapter
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public int Order
        {
            get
            {
                return 1;
            }
        }

        private const string CONTEXT_NAME = "sdf_sapadapter_context";

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder, SDFConfig config)
        {
            builder.RegisterType<SAPConnectorService>().As<ISAPConnectorService>().InstancePerLifetimeScope();
            builder.RegisterType<SAPInstanceService>().As<ISAPInstanceService>().InstancePerLifetimeScope();

            //data context
            this.RegisterPluginDataContext<DataContextSAPAdapter>(builder, CONTEXT_NAME);

            //override required repository with our custom context
            builder.RegisterType<EfRepository<SAPInstance>>()
               .As<IRepository<SAPInstance>>()
               .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
               .InstancePerLifetimeScope();

            //builder.RegisterType<EfRepository<SAPAdapterItem>>()
            //   .As<IRepository<SAPAdapterItem>>()
            //   .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))
            //   .InstancePerLifetimeScope();

        }
    }
}